package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.UserMemberOrder;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import com.ruoyi.order.service.IUserMemberOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 会员订单Controller
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/userMemberOrder/userMemberOrder")
public class UserMemberOrderController extends BaseController {
    @Autowired
    private IUserMemberOrderService userMemberOrderService;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询会员订单列表
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserMemberOrder userMemberOrder) {
        if (!CheckEmptyUtil.isEmpty(userMemberOrder.getUserName())){
            List<Long> userIds = technicianItemOrderMapper.seleUserId(userMemberOrder.getUserName());
            if (!CheckEmptyUtil.isEmpty(userIds)){
                userMemberOrder.setUserIds(userIds);
            }else {
                return getDataTable(Collections.emptyList());
            }
        }else {
            userMemberOrder.setUserIds(Collections.emptyList());
        }
        startPage();
        List<UserMemberOrder> list = userMemberOrderService.selectUserMemberOrderList(userMemberOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserMemberOrder order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出会员订单列表
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:export')")
    @Log(title = "会员订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserMemberOrder userMemberOrder) {
        if (!CheckEmptyUtil.isEmpty(userMemberOrder.getUserName())){
            List<Long> userIds = technicianItemOrderMapper.seleUserId(userMemberOrder.getUserName());
            if (!CheckEmptyUtil.isEmpty(userIds)){
                userMemberOrder.setUserIds(userIds);
            }else {
                userMemberOrder.setUserIds(Collections.emptyList());
            }
        }else {
            userMemberOrder.setUserIds(Collections.emptyList());
        }
        List<UserMemberOrder> list = userMemberOrderService.selectUserMemberOrderList(userMemberOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (UserMemberOrder order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
            }
        }
        ExcelUtil<UserMemberOrder> util = new ExcelUtil<UserMemberOrder>(UserMemberOrder.class);
        util.exportExcel(response, list, "会员订单数据");
    }

    /**
     * 获取会员订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        UserMemberOrder userMemberOrder = userMemberOrderService.selectUserMemberOrderById(id);
        if (!CheckEmptyUtil.isEmpty(userMemberOrder)) {

            String userName = technicianItemOrderMapper.selectUserName(userMemberOrder.getUserId());
            userMemberOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);

        }
        return success(userMemberOrder);
    }

    /**
     * 新增会员订单
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:add')")
    @Log(title = "会员订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserMemberOrder userMemberOrder) {
        return toAjax(userMemberOrderService.insertUserMemberOrder(userMemberOrder));
    }

    /**
     * 修改会员订单
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:edit')")
    @Log(title = "会员订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserMemberOrder userMemberOrder) {
        return toAjax(userMemberOrderService.updateUserMemberOrder(userMemberOrder));
    }

    /**
     * 删除会员订单
     */
    @PreAuthorize("@ss.hasPermi('userMemberOrder:userMemberOrder:remove')")
    @Log(title = "会员订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userMemberOrderService.deleteUserMemberOrderByIds(ids));
    }
}
